package com.example.nbastar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.nbastar.bean.User;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG ="RegistActivity";
    EditText mEtName,mEtPwd;
    Button mBtnRegist;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }
    private void initView() {
        mEtName = findViewById(R.id.et_name);
        mEtPwd = findViewById(R.id.et_password);
        mBtnRegist = findViewById(R.id.btn_regist);
        mBtnRegist.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_regist:
                String name = mEtName.getText().toString().trim();
                String password = mEtPwd.getText().toString().trim();

                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
                    Toast.makeText(this, "请输入完整", Toast.LENGTH_SHORT).show();
                    return;
                }
                OkHttpClient client = new OkHttpClient();
                JSONObject object = new JSONObject();
                    try {
                        object.put("name", name);
                        object.put("password", password);
                        object.put("email","yx");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String json = object.toString();

                    RequestBody body = FormBody.create(MediaType.parse("application/josn;charset=UTF-8"), json);
                    Request request = new Request.Builder()
                            .url("http://192.168.43.21:8080/nbaserver/user/regist")
             //   http://localhost:8080/nbaserver/user/login
                            .post(body)
                            .build();
                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            Log.e(TAG, "onFailure: " + e);
                        }
                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            String jsonStr = response.body().string();
                            Log.e(TAG, "onResponse: " + jsonStr);


                        }
                    });
                    finish();
                    Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
                    startActivity(intent);
                    break;
        }
    }

}